<template>
  <div>
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getGoodsDataList(activityId)">
      <el-form-item>
        <el-input v-model="dataForm.goodsName" placeholder="商品名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getGoodsDataList(activityId)">查询</el-button>
        <el-button @click="dataForm.goodsName=''
         getGoodsDataList(activityId)" type="info" plain >重置</el-button>
        <el-button v-if="isAuth('spike:goods:add')" type="primary" @click="addOrUpdateHandle()">添加商品</el-button>
        <el-button v-if="isAuth('spike:goods:remove')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <!--<el-table-column-->
        <!--prop="activityId"-->
        <!--header-align="center"-->
        <!--align="center"-->
        <!--label="活动id">-->
      <!--</el-table-column>-->
      <!--<el-table-column-->
        <!--prop="id"-->
        <!--header-align="center"-->
        <!--align="center"-->
        <!--label="商品ID">-->
      <!--</el-table-column>-->
      <el-table-column
        prop="goodsName"
        header-align="center"
        align="center"
        label="商品名称">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" :content="scope.row.goodsName" placement="right-start">
            <span class="characters">{{scope.row.goodsName}}</span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column
        prop="goodsSaleNum"
        header-align="center"
        align="center"
        label="销量">
      </el-table-column>
      <el-table-column
        prop="goodsImages"
        header-align="center"
        align="center"
        label="商品图">
        <template slot-scope="scope">
          <el-popover
            placement="right"
            title=""
            trigger="hover">
            <img :src="`http://image.yilianbaihui.cn/consumer/${scope.row.goodsImages}`" style="max-width: 300px"/>
            <img slot="reference" :src="`http://image.yilianbaihui.cn/consumer/${scope.row.goodsImages}`" :alt="`http://image.yilianbaihui.cn/consumer/${scope.row.goodsImages}`" style="max-height: 30px;max-width: 80px"/>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column
        prop="goodsType"
        header-align="center"
        align="center"
        label="类型">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.priceType === '1'" type="success">兑换商城</el-tag>
          <el-tag v-if="scope.row.priceType === '2'">福利商城</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="vipPrice"
        header-align="center"
        align="center"
        label="商城售价">
      </el-table-column>
      <el-table-column
        prop="goodsIntegral"
        header-align="center"
        align="center"
        label="积分">
      </el-table-column>
      <el-table-column
        prop="goodsSequence"
        header-align="center"
        align="center"
        label="排序">
      </el-table-column>
      <el-table-column
        prop="activityGoodsInventoryDetail"
        header-align="center"
        align="center"
        width="100"
        label="规格">
        <template slot-scope="scope">
          <el-button slot="reference" size="mini"
                     @click="specsHandle(scope.row.id,scope.row.activityGoodsInventoryDetail,scope.row.priceType === '1')">查看规格</el-button>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="100"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="updateHandle(scope.row.id,scope.row.goodsSequence,scope.row.goodsSaleNum)">修改</el-button>
          <el-button type="text" size="small" v-if="isAuth('spike:goods:remove')" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getGoodsDataList"></add-or-update>
    <specs-add-or-update v-if="dialogVisible" ref="specsAddOrUpdate" @refreshDataList="getGoodsDataList"></specs-add-or-update>


    <el-dialog title="修改" :visible.sync="sequenceVisible" width="20%">
      <el-form ref="form" :model="updateForm" label-width="70px">
        <el-form-item label="排序">
          <el-input v-model="updateForm.updateSequence" property="排序"></el-input>
        </el-form-item>
        <el-form-item label="销量">
          <el-input v-model="updateForm.updateSaleNum" property="销量"></el-input>
        </el-form-item>
      </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="sequenceVisible = false">取 消</el-button>
          <el-button type="primary" @click="sequenceUpdate()">确 定</el-button>
        </span>
    </el-dialog>
  </div>
</template>

<script>
  import AddOrUpdate from './goods-add-or-update'
  import SpecsAddOrUpdate from './specs-add-or-update'
  export default {
    data () {
      return {
        dialogVisible: false,
        dataForm: {
          goodsName: ''
        },
        activityId: 0,
        goodsSkuList: '[]',
        spikeActivityId: null,
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        sequenceVisible: false,
        updateForm: {
          updateId: 0,
          updateSequence: '',
          updateSaleNum: ''
        }
      }
    },
    components: {
      AddOrUpdate,
      SpecsAddOrUpdate
    },
    activated () {
    },
    methods: {
      // 获取数据列表
      getGoodsDataList (id) {
        if (id != null) {
          this.activityId = id
        }
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/activity/spikeGoods/queryActivityGoodsList'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'key': this.activityId,
            'goodsName': this.dataForm.goodsName
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.data.list
            this.totalPage = data.data.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      sequenceUpdate () {
        this.$http({
          url: this.$http.adornUrl('/activity/spikeGoods/modify'),
          method: 'post',
          data: this.$http.adornData({
            'id': this.updateForm.updateId,
            'goodsSequence': this.updateForm.updateSequence,
            'goodsSaleNum': this.updateForm.updateSaleNum
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getGoodsDataList(this.activityId)
              }
            })
          } else {
            this.$message.error(data.msg)
          }
          this.sequenceVisible = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getGoodsDataList(this.activityId)
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getGoodsDataList(this.activityId)
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 修改排序
      updateHandle (id, val, val1) {
        this.sequenceVisible = true
        this.updateForm.updateId = id
        this.updateForm.updateSequence = val
        this.updateForm.updateSaleNum = val1
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/activity/spikeGoods/remove'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getGoodsDataList(this.activityId)
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      specsHandle (id, val, goodsType) {
        this.dialogVisible = true
        this.$nextTick(() => {
          this.$refs.specsAddOrUpdate.init(id, JSON.parse(val), true, goodsType)
        })
      }
    }
  }
</script>
<style>
  .characters{
    width:300px;
    white-space:nowrap;
    overflow:hidden;
    text-overflow:ellipsis;
  }
</style>
